package com.yanqu.road.server.manger;

import com.yanqu.road.entity.config.goods.GoodsBagInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.logic.bussiness.config.GoodsBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class TempGoodsBagMgr extends TempMgr {

	private static Map<Integer, Map<Integer, List<GoodsBagInfo>>> goodsBagMap;

	private static RandomHelper random = new RandomHelper();

	@Override
	public boolean reloadConfig() {
		reloadGoodsBagMap();
		return true;
	}

	@Override
	public boolean reloadData() throws Exception {
		return true;
	}

	public static void reloadGoodsBagMap() {
		goodsBagMap = GoodsBussiness.getGoodsBagInfoMap();
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean stop() {
		return true;
	}

	@Override
	public boolean save() {
		return false;
	}

	/**
	 * 获取礼包里面随机道具
	 */
	public static OpenGoodsBagResult getRandomGoods(int bagId) {
		List<GoodsBagInfo> goodsList = getRandomGoodsList(bagId);
		if(null == goodsList){
			goodsList = TempCommonMgr.getRandomGoodsList(bagId);
		}
		return getRandomGoods(goodsList);
	}

	public static List<OpenGoodsBagResult> getMultipleRandomGoodsNoRepeat(int bagId, int randomCount) {
		List<List<GoodsBagInfo>> randomResultList = getMultipleRandomGoodsList(bagId, randomCount);
		if (randomResultList == null) {
			randomResultList = TempCommonMgr.getMultipleRandomGoodsList(bagId, randomCount);
		}
		List<OpenGoodsBagResult> list = new ArrayList<>();
		if (randomResultList == null) {
			return list;
		}
		for (List<GoodsBagInfo> bagInfoList : randomResultList) {
			list.add(getRandomGoods(bagInfoList));
		}
		return list;
	}

	private static OpenGoodsBagResult getRandomGoods(List<GoodsBagInfo> goodsBagInfoList) {
		OpenGoodsBagResult result = new OpenGoodsBagResult();
		if(null == goodsBagInfoList) {
			return result;
		}
		Property reward = new Property();
		for(GoodsBagInfo goodsBagInfo : goodsBagInfoList) {
			if (result.getNotice() == 0) {
				result.setNotice(goodsBagInfo.getNotice());
			}
			reward.addProperty(goodsBagInfo.getGoodsId(), BigInteger.valueOf(random.next(goodsBagInfo.getMinNum(), goodsBagInfo.getMaxNum() + 1)));
		}
		result.setReward(reward);
		return result;
	}

	/**
	 * 获取礼包里面随机道具信息列表
	 */
	private static List<GoodsBagInfo> getRandomGoodsList(int bagId) {
		List<GoodsBagInfo> resultGoodsList = new ArrayList<>();
		Map<Integer, List<GoodsBagInfo>> goodsMap = goodsBagMap.get(bagId);
		if(null == goodsMap) {
			return null;
		}
		for(List<GoodsBagInfo> goodsList : goodsMap.values()){
			int sumWeight = 0;
			for(GoodsBagInfo goodsBagInfo : goodsList){
				sumWeight += goodsBagInfo.getRandomWeight();
			}
			if(sumWeight > 0){
				int randomWeight = random.next(0, sumWeight);
				int tempWeight = 0;
				for(GoodsBagInfo goodsBagInfo : goodsList){
					tempWeight += goodsBagInfo.getRandomWeight();
					if(tempWeight > randomWeight){
						resultGoodsList.add(goodsBagInfo);
						break;
					}
				}
			}
		}
		return resultGoodsList;
	}

	private static List<List<GoodsBagInfo>> getMultipleRandomGoodsList(int bagId, int randomCount) {
		if (!goodsBagMap.containsKey(bagId)) {
			return null;
		}
		List<List<GoodsBagInfo>> resultList = new ArrayList<>();
		Map<Integer, List<GoodsBagInfo>> goodsMap = new HashMap<>(goodsBagMap.get(bagId));
		List<List<GoodsBagInfo>> randomList = new ArrayList<>();
		for (List<GoodsBagInfo> list : goodsMap.values()) {
			randomList.add(new ArrayList<>(list));
		}
		for (int i = 0; i < randomCount; i++) {
			List<GoodsBagInfo> randomResult = new ArrayList<>();
			for (List<GoodsBagInfo> bagInfoList : randomList) {
				int sumWeight = 0;
				for (GoodsBagInfo goodsBagInfo : bagInfoList) {
					sumWeight += goodsBagInfo.getRandomWeight();
				}
				if (sumWeight > 0) {
					int randomWeight = random.next(0, sumWeight);
					int tempWeight = 0;
					Iterator<GoodsBagInfo> iterator = bagInfoList.iterator();
					while (iterator.hasNext()) {
						GoodsBagInfo bagInfo = iterator.next();
						tempWeight += bagInfo.getRandomWeight();
						if (tempWeight > randomWeight) {
							randomResult.add(bagInfo);
							iterator.remove();
							break;
						}
					}
				}
			}
			if (randomResult.size() > 0) {
				resultList.add(randomResult);
			}
		}
		return resultList;
	}

	public static void testRandom() {
		goodsBagMap = new HashMap<>();
		int goodsId = 200009;
		for (int i = 0; i < 10; i++) {
			GoodsBagInfo bagInfo = new GoodsBagInfo();
			bagInfo.setBagId(119151);
			bagInfo.setGoodsId(goodsId + i);
			bagInfo.setRandomType(1);
			bagInfo.setRandomWeight(1);
			if (!goodsBagMap.containsKey(bagInfo.getBagId())) {
				goodsBagMap.put(bagInfo.getBagId(), new ConcurrentHashMap<>());
			}
			if (!goodsBagMap.get(bagInfo.getBagId()).containsKey(bagInfo.getRandomType())) {
				goodsBagMap.get(bagInfo.getBagId()).put(bagInfo.getRandomType(), new ArrayList<>());
			}
			goodsBagMap.get(bagInfo.getBagId()).get(bagInfo.getRandomType()).add(bagInfo);
		}
	}

	public static void main(String[] args) {
		testRandom();
		getMultipleRandomGoodsList(119151, 110);
	}
}
